/**
 *
 */
package com.yangtao.prototype.system.dao;

import com.yangtao.framework.common.Option;
import com.yangtao.framework.hibernate.BaseDao;
import com.yangtao.framework.util.StringHelper;
import com.yangtao.prototype.system.domain.DataAuthority;
import com.yangtao.prototype.system.domain.User;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.hibernate.type.StandardBasicTypes;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 */
@Repository
public class UserDao extends BaseDao<User> {

	/**
	 * 增加自定义的筛选条件
	 *
	 * @param criteria
	 */
	protected void addCustomCriterion(Criteria criteria, Object... parameters) {
		if (parameters != null && parameters.length > 0) {
			if (parameters[0] != null) {
				String organizePath = parameters[0].toString();
				//增加数据权限的约束原生sql的约束
				criteria.add(Restrictions.sqlRestriction("exists (select 1 from sys_organization org where org.id = {alias}.organizationId and org.path like ?)",
						organizePath + "%", StandardBasicTypes.STRING));
			}
		}
	}

	/**
	 * 取到用户的数据权限
	 *
	 * @param user 用户
	 * @return 数据权限键值对
	 */
	public Map<String, List<String>> findUserDataAuth(String authKey, User user) {
		Map<String, List<String>> map = new HashMap<String, List<String>>();
		for (DataAuthority da : user.getDataAuth()) {
			if (map.get(da.getResourceType()) == null)
				map.put(da.getResourceType(), new ArrayList<String>());
			List<String> darr = map.get(da.getResourceType());
			darr.add(da.getResourceId());
		}
		return map;
	}

	/**
	 * 查询用户已经分配的角色
	 *
	 * @param userId 用户ID
	 * @return
	 */
	public List<Option> findDistributeRoles(String userId) {
		if (StringHelper.isEmpty(userId)) {
			// 已分配角色
			return null;
		} else {
			// 已分配角色
			String hql1 = "select new com.yangtao.framework.common.Option(ur.role.id,ur.role.roleName) from UserRole ur where ur.user.id=?";
			return find(hql1, userId);
		}
	}

	/**
	 * 查询用户未分配的角色
	 *
	 * @param userId 用户ID
	 * @return
	 */
	public List<Option> findUnDistributeRoles(String userId, String organizePath) {
		Assert.hasText(organizePath, "组织机构路径不能为空");
		StringBuilder hql = new StringBuilder("select new com.yangtao.framework.common.Option(r.id,r.roleName) from Role r");
		hql.append(" where r.active = 1");
		hql.append(" and exists (select 1 from Organization org where org.id = r.organizationId and org.path like ?)");
		//TODO 增加其他筛选条件
		if (StringHelper.isEmpty(userId)) {
			return find(hql.toString(), organizePath + "%");
		} else {
			hql.append(" and r not in (select ur.role from UserRole ur where ur.user.id=?)");
			return find(hql.toString(), organizePath + "%", userId);
		}

	}

	/**
	 * 删除用户的角色
	 *
	 * @param userId
	 */
	public void deleteUserRoles(String userId) {
		batchExecute("delete from UserRole ur where ur.user.id=?", userId);
	}
}
